package cn.gdcp.listviewtest;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class StudentAdapter extends ArrayAdapter<Student> {
    private int resourceId;
   public StudentAdapter(Context context, int textViewResourceId, List<Student> objects){
       super(context,textViewResourceId,objects);
       resourceId=textViewResourceId;
   }

    @NonNull
    @Override
    public View getView(int position,  View convertView,  ViewGroup parent) {
       Student student=getItem(position);
        LayoutInflater inflater=LayoutInflater.from(getContext());
        View view= inflater.inflate(resourceId,parent,false);
       ImageView studentImg=(ImageView)view.findViewById(R.id.student_img);
        TextView studentName=(TextView)view.findViewById(R.id.student_name);
        TextView studentAge=(TextView)view.findViewById(R.id.student_age);
        studentName.setText(student.getName());
         studentImg.setImageResource(student.getImgId());
        studentAge.setText(student.getAge()+"");
        return  view;
    }
}
